﻿namespace DiffLCS

open System
open System.Threading.Tasks

module private TaskHelper =
    // Creates a Task with an Action executing the function action
    let inline task action =
        new Task(new Action(action))

    let inline taskf (func : unit -> 'a) =
        new Task<'a>(new Func<'a>(func))

    // Returns a task which continue after "task"
    let inline (-->) (task : Task) (next : unit -> unit) =
        task.ContinueWith(fun (q : Task) -> next ())

    let inline (-->>) (task : Task<'a>) (next : 'a -> 'a) =
        task.ContinueWith(fun (q : Task<'a>) -> next q.Result)

    // Returns a task which continue after all the tasks are completed
    let inline (>->) (tasks : Task[]) (next : unit -> unit) =
        Task.Factory.ContinueWhenAll(tasks, fun (qs : Task[]) -> next ())

    let inline (>->>) (tasks : Task<'a>[]) (next : 'a[] -> 'a) =
        Task.Factory.ContinueWhenAll(tasks, fun (qs : Task<'a>[]) -> next <| Array.map (fun (t : Task<'a>) -> t.Result) tasks)